<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="loginForm" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" show-password />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberMe"> 记住我 </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button class="login_btn" type="primary" @click="login">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { LOGINFORM_KEY } from '@/constants/KEY'
import { resetRouter } from '@/router'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rememberMe: true,
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created () {
    // 记住我 回显
    const formData = localStorage.getItem(LOGINFORM_KEY)
    if (formData) {
      const { username, password } = JSON.parse(formData)
      this.loginForm.username = username
      this.loginForm.password = password
    }
  },
  methods: {
    login () {
      this.$refs['form'].validate(async valid => {
        if (!valid) return
        // 调用vuex axtions 是异步代码，所以加上await，让当前执行顺序变为同步
        await this.$store.dispatch('user/login', this.loginForm)
        if (this.rememberMe) {
          localStorage.setItem(LOGINFORM_KEY, JSON.stringify(this.loginForm))
        } else {
          localStorage.removeItem(LOGINFORM_KEY)
        }

        // 知识点：this.$route.query.redirect 常用于登录后自动跳转页面。
        // 说明：使用rediurect属性的时候必须要有地方先给他赋值，如退出登录时使用：this.$router.push(`/login?redirect=${this.$route.fullPath}`)，
        // push参数里面的/login是可以调用redirect值的界面
        // this.$router.push('/')
        if (this.$route.query.redirect) {
          //默认打开上次页面（需要判断用户是否有权限打开界面，否则跳转登录页） 【这个判断逻辑可不写，后续当练习】
          this.$router.push(this.$route.query.redirect).catch(() => {})
        } else {
          this.$router.push('/').catch(() => {})
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
}
.bg {
  width: 60vw;
  height: 100vh;
  background: url('~@/assets/login-bg.svg') no-repeat;
  background-position: right top;
  background-size: cover;
}
.box {
  margin: 200px 10% 0;
  flex: 1;
  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }
    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }
    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}
.login_btn {
  width: 100%;
}
::v-deep .el-input__inner {
  font-family: 'Microsoft YaHei', sans-serif;
}
</style>
